#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"

// 定义心形曲线的方程
double heartShape_x(double t) {
    return sqrt(3) * cos(t);
}

double heartShape_y(double t) {
    return 2 * sqrt(3) * sin(t) / 3 + 2 * pow(3, 0.25) / 3 * sqrt(std::fabs(cos(t)));
}

// 生成贝塞尔曲线的控制点
std::vector<std::vector<double>> generateBezierControlPoints(int m) {
    std::vector<std::vector<double>> controlPoints;
    double delta = 2.0 * M_PI / m; 
    for (int i = 0; i <= m; ++i) {
        double t = i * delta + M_PI / 2;
        double x = heartShape_x(t);
        double y = heartShape_y(t);
        controlPoints.push_back({x, y});
        //std::cout << "Bezier interpolation at t=" << t << ": " << controlPoints.back()[0] << "," << controlPoints.back()[1] << std::endl;
    }

    return controlPoints;
}

std::vector<double> polynomial(double t, int m) {
    std::vector<std::vector<double>> vec;
    vec = generateBezierControlPoints(m);
    BezierInterpolation bezier(vec);
    return bezier.interpolate_vector(t);  
}

int main() {
    std::vector<int> array = {10, 40, 160};
    for (int m : array) {
        std::ofstream file("data_F" + std::to_string(m) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double t = 0; t <= 1; t += 0.01) {
            std::vector<double> data;
            data = polynomial(t, m);
            file << data[0] << "," << data[1] << std::endl;
        }

        file.close();
    }
    std::ofstream file("data_F.csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }
        std::vector<std::vector<double>> matrix;
        matrix = generateBezierControlPoints(10000);
        for (std::vector<double> d : matrix) {
            file << d[0] << "," << d[1] << std::endl;
        }

        file.close();

    return 0; 
}